package com.xing.web.controller;

import com.xing.domain.Account;
import com.xing.service.AccountService;
import com.xing.service.impl.AccountServiceImpl;
import com.xing.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 * 静，谦虚谨慎，心胸宽广容纳所有好的坏的、先苦后甜
 * day41_transaction
 * 2021/5/8 17:32
 */
@WebServlet(value = "/loginservlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //1.获取数据
        String cardNo = req.getParameter("cardNo");
        String pwd = req.getParameter("pwd");

        //2.校验
        if(StringUtils.isEmptry(cardNo)){
            req.setAttribute("msg", "卡号不能为空");
            req.getRequestDispatcher("/message.jsp").forward(req, resp);
            return;
        }
        if(StringUtils.isEmptry(cardNo)){
            req.setAttribute("pwd", "密码不能为空");
            req.getRequestDispatcher("/message.jsp").forward(req, resp);
            return;
        }
        //3.创建业务对象
        AccountService accountService=new AccountServiceImpl();
        try {
            Account account=accountService.login(cardNo,pwd);
            //把账户放入session中
            req.getSession().setAttribute("account", account);
            resp.sendRedirect(req.getContextPath()+"/index.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("mag", "登录失败");
            req.getRequestDispatcher("/message.jsp").forward(req, resp);
        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
